/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import fsahoraires.cours.Cours;
import fsahoraires.programmes.Programme;
import fsahoraires.programmes.Semestre;

public class SemestrePage extends SimpleHtmlTemplatePage {

	private final Programme programme;
	private final Semestre semestre;

	public SemestrePage(Programme programme, Semestre semestre) {
		super("Aperçu", "semestre.png", "programme.xhtml");
		this.programme = programme;
		this.semestre = semestre;
	}

	@Override
	protected String loadContent() {
		StringBuilder str = new StringBuilder();
		addSemestre(str, semestre, programme);
		return str.toString();
	}

	/**
	 * Ajoute l'aperçu d'un semestre donné à un StringBuilder
	 * 
	 * @param str
	 * @param sem
	 * @param prog programme contenant ce semestre et les autres
	 */
	public static void addSemestre(StringBuilder str, Semestre sem,
			Programme prog) {
		str.append("<h2>Année ").append(sem.getAnnee()).append(" quadrimestre ")
				.append(sem.getSemestre()).append("</h2>");
		str.append("<table class='cours'>");
		int i = 0;
		for (Cours c : sem.getCours()) {
			str.append("<tr");
			if (i % 2 == 0)
				str.append(">");
			else
				str.append(" class='second'>");
			str.append("<td class='code'>").append(c.getCode());
			str.append("</td><td class='titre'>").append(c.getTitre());
			str.append("</td><td class='credits'>").append(c.getCredits());
			str.append(" crédits</td><td class='heures'>");
			str.append(c.getHeures()).append("h</td></tr>");
			i++;
		}
		str.append("</table><div class='heures'><span class='value'>").append(
				sem.getHeuresParSemaine()).append(
				"</span> heures par semaine</div>");
		str.append("<div class='soustotal'>Sous-total: <span class='value'>")
				.append(sem.getCredits()).append("</span> crédits</div>");
	}

}
